package com.action.mvc.sm.service;

import com.action.mvc.sm.mapper.BooksMapper;
import com.action.mvc.sm.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wuxiang on 2019/5/10.
 */
@Service
public class BooksService {

    @Autowired
    BooksMapper booksMapper;

    /**
     * 在  config/spring/applicationContext-transaction.xml 文件中
     * 使用方式1，需要加 @Transactional 注解，才能保持一致性
     * 使用方式2，不需要加 @Transactional 注解
     * @throws Exception
     */
    @Transactional
    public void update() throws Exception {
        Books books1 = booksMapper.selectByPrimaryKey(1);
        Books books2 = booksMapper.selectByPrimaryKey(2);
        Books books3 = booksMapper.selectByPrimaryKey(3);

        books1.setPrice(1);
        books2.setPrice(2);
        books3.setPrice(3);

        booksMapper.updateByPrimaryKey(books1);

        if (true) {
            throw new Exception("12312321");
        }

        booksMapper.updateByPrimaryKey(books2);
        booksMapper.updateByPrimaryKey(books3);
    }
}
